package com.xbongbong.pro.rabbitmq.thread;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ScoreThreadPoolBeanConfig {
    /**
     * 评分“重新计算”的线程池
     */
    @Bean(name = "scoreScriptThreadPool")
    public TaskExecutor scoreThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数,默认为1
        executor.setCorePoolSize(10);
        // 设置最大线程数，默认为Integer.MAX_VALUE
        executor.setMaxPoolSize(20);
        // 设置线程池维护线程所允许的空闲时间（秒），默认为60s
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("score-");
        /*
        设置拒绝策略
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 执行初始化
        executor.initialize();
        return executor;
    }
}
